dojo.provide("lotex.report.Display");
dojo.require("lotex.string");
dojo.require("lotex.math");

lotex.report.Display = {
    process: function(sheet) {
        //b(sheet);
        var cfg = sheet.getConfig();
        if(cfg.alternateRow)
            this.setAlternateRowClass(sheet);
        // TODO optimize
        var y = sheet.getRowLen();
        var x = sheet.getColLen();
        for(var row=0; row<y; row++) {
            for(var col=0; col<x; col++) {
                var cell = sheet.rows[row].cells[col];
                var value = cell.value;
                if(lotex.array.has(cell._classnames,"money")) {
                    cell.value = this.asMoney(value, 2);
                }
//b();
                if(cell.getConfigItem("merge") && !cell.beMerge) {
                    var mm = row + 1;
                    while (mm < y)
                    {
                        var compareCell = sheet.rows[mm].cells[col];
                        //dojo.debug("v1: "+v1.value + " v2: "+v2.value + " start: "+ start + " j: "+j);
                        if(compareCell.value==value) {
                            compareCell.beMerge = true;
                            mm++;
                        } else {
                            break;
                        }
                    }
                    cell.rowSpan += mm - row - 1;
                }
            }
        }
    },

    asMoney: function(str, bit) {
        var num = parseFloat(str);
        // TODO Fix isNaN ,dojo.isNumber
        if (isNaN(num)) {return str;}
        var s = lotex.math.roundEx(num, bit, true) + "";
        for(var i = s.indexOf(".") - 3; i>0; i-=3) {
            s = lotex.string.splice(s,i,0,",");
        }
        return "￥" + s;
    },

    setAlternateRowClass: function(sheet) {
        for(var i=0; i<sheet.rows.length; i+=2) {
            sheet.rows[i].addClassName("a");
        }
    }
}